package com.example.petshopping.ui.pet;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.model.PetProduct;
import com.example.petshopping.ui.StoreRankingActivity;
import com.example.petshopping.ui.adapter.PetProductAdapter;
import com.example.petshopping.ui.main.MainActivity;
import com.google.android.material.chip.Chip;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import com.example.petshopping.ui.aquarium.AquariumActivity;
import com.example.petshopping.ui.fixedprice.FixedPriceActivity;

public class PetActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private PetProductAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet);
        
        // 设置状态栏高度
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            findViewById(R.id.status_bar_bg).getLayoutParams().height = statusBarHeight;
        }
        
        // 初始化RecyclerView
        recyclerView = findViewById(R.id.pet_product_list);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        
        // 创建和设置适配器
        List<PetProduct> productList = createSampleProducts();
        adapter = new PetProductAdapter(this, productList);
        recyclerView.setAdapter(adapter);
        
        // 设置导航标签的点击监听
        setupNavigation();
        
        // 设置点击事件
        setupClickListeners();
    }
    
    private void setupNavigation() {
        TabLayout navMenu = findViewById(R.id.nav_menu);
        navMenu.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                // 如果不是点击当前页面对应的标签，则进行页面跳转
                if (position != 1) { // 宠物标签的位置是1
                    switch (position) {
                        case 0: // 首页
                            startActivity(new Intent(PetActivity.this, MainActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 2: // 水族
                            startActivity(new Intent(PetActivity.this, AquariumActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 3: // 一口价
                            startActivity(new Intent(PetActivity.this, FixedPriceActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 4: // 同城
                            startActivity(new Intent(PetActivity.this, com.example.petshopping.ui.samecity.SameCityActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 不需要处理
            }
        });
        
        // 设置默认选中的标签为宠物
        navMenu.getTabAt(1).select();
    }
    
    private void setupClickListeners() {
        // 搜索输入框点击跳转到搜索页
        findViewById(R.id.search_input).setOnClickListener(v -> {
            Intent intent = new Intent(PetActivity.this, com.example.petshopping.ui.search.SearchActivity.class);
            overridePendingTransition(0,0);
            startActivity(intent);
        });
        // 搜索按钮点击事件
        findViewById(R.id.search_button).setOnClickListener(v -> {
            // 原有Snackbar可保留或删除
            // Snackbar.make(v, "搜索功能即将上线", Snackbar.LENGTH_SHORT).show();
        });
        // 参与按钮点击事件
        findViewById(R.id.participate_btn).setOnClickListener(v -> {
            Snackbar.make(v, "您点击了参与按钮！", Snackbar.LENGTH_SHORT).show();
        });
        // 添加标签按钮点击事件
        Chip addTagBtn = findViewById(R.id.add_tag_btn);
        addTagBtn.setOnClickListener(v -> {
            Snackbar.make(v, "即将进入自定义标签页面", Snackbar.LENGTH_SHORT).show();
        });
        
        // 设置底部导航栏点击事件
        setupBottomNavigation();
        
        // 店铺排行点击事件
        findViewById(R.id.store_ranking_layout).setOnClickListener(v -> {
            Intent intent = new Intent(PetActivity.this, StoreRankingActivity.class);
            overridePendingTransition(0,0);
            startActivity(intent);
        });
    }
    
    private void setupBottomNavigation() {
        // 获取底部导航栏
        com.google.android.material.bottomnavigation.BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        
        // 设置当前选中项为宠物对应的首页
        bottomNav.setSelectedItemId(R.id.navigation_home);
        
        // 设置点击监听器
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    // 当前已在首页/宠物页
                    return true;
                case R.id.navigation_category:
                    // 跳转到分类页面
                    startActivity(new Intent(PetActivity.this, com.example.petshopping.ui.category.CategoryActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_message:
                    // 跳转到消息页面
                    startActivity(new Intent(this, com.example.petshopping.ui.message.MessageActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_profile:
                    // 跳转到我的页面
                    startActivity(new Intent(this, com.example.petshopping.ui.profile.ProfileActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
            }
            return false;
        });
        
        // 设置中间按钮点击事件
        findViewById(R.id.center_button).setOnClickListener(v -> {
            Snackbar.make(v, "中间按钮功能即将上线", Snackbar.LENGTH_SHORT).show();
        });
    }
    
    private List<PetProduct> createSampleProducts() {
        List<PetProduct> products = new ArrayList<>();
        
        // 添加样本数据
        products.add(new PetProduct(
            "纯种英短蓝猫幼崽 疫苗已做 可上门看猫", 
            1200, 
            R.drawable.cat, 
            "无理由", 
            "今天20:05截拍", 
            "喵星人旗舰店"
        ));
        
        products.add(new PetProduct(
            "纯种金毛幼犬 2个月大 已注射疫苗 健康活泼", 
            1800, 
            R.drawable.dog, 
            "无理由", 
            "明天12:00截拍", 
            "汪汪之家"
        ));
        
        products.add(new PetProduct(
            "澳洲牡丹鹦鹉 会说话 性格温顺 适合新手饲养", 
            2500, 
            R.drawable.yingwu, 
            "无理由", 
            "后天10:30截拍", 
            "鸟语花香"
        ));
        
        products.add(new PetProduct(
            "布偶猫幼崽 纯种海豹双色 亲人可爱 健康无病", 
            3600, 
            R.drawable.cat, 
            "无理由", 
            "今天18:00截拍", 
            "萌宠之家"
        ));
        
        return products;
    }
    
    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
} 